﻿using System;
using System.Collections.Generic;
using System.Linq;
using Microsoft.Xna.Framework;
using Microsoft.Xna.Framework.Audio;
using Microsoft.Xna.Framework.Content;
using Microsoft.Xna.Framework.GamerServices;
using Microsoft.Xna.Framework.Graphics;
using Microsoft.Xna.Framework.Input;
using Microsoft.Xna.Framework.Media;

namespace FirstGame
{
    public class MapWithCell : VisibleGameEntity
    {
        private Vector2 _clientBound;
        private Vector2 SPEED_SCROLL = new Vector2(20,20);
        private int _nRow;
        private int _nCol;
        private Vector2 CELL_SIZE = new Vector2(97, 49);
        private MapCell[,] _Cell;

        public MapWithCell(ContentManager content, string strTexture, int nrow, int ncol, Vector2 topleft, Vector2 clientbound)
        {
            _clientBound = clientbound;
            _TopLeft = topleft;
            _nRow = nrow;
            _nCol = ncol;
            _Cell = new MapCell[_nRow,_nCol];
            for (int i = 0; i < _nRow; i++)
            {
                for (int j = 0; j < _nCol; j++)
                {
                    float x = (i % 2) * ((int)CELL_SIZE.X >> 1) + j * CELL_SIZE.X - TopLeft.X;
                    float y = i * ((int)CELL_SIZE.Y >> 1) - TopLeft.Y;
                   
                   
                    string str = strTexture + int.Parse(i.ToString() + j.ToString()).ToString("0000");
                    _Cell[i, j] = new MapCell(content, str, new Vector2(x, y), CELL_SIZE);
                }   
            }
        }

        public override void Update(GameTime gameTime, InputSystem input)
        {
            Vector2 viewport = this.TopLeft;

            if (input.CurPoint.X <= 0)
            {
                viewport.X -= SPEED_SCROLL.X;
                if(viewport.X < 0)
                {
                    viewport.X = 0;
                }
            }

            if (input.CurPoint.Y <= 0)
            {
                viewport.Y -= SPEED_SCROLL.Y;
                if (viewport.Y < 0)
                {
                    viewport.Y = 0;
                }
            }
            if (input.CurPoint.X >= _clientBound.X - 5)
            {
                viewport.X += SPEED_SCROLL.X;
                if (viewport.X > (_nCol * CELL_SIZE.X + ((int)CELL_SIZE.X >> 1) - _clientBound.X))
                {
                    viewport.X = (_nCol * CELL_SIZE.X + ((int)CELL_SIZE.X >> 1) - _clientBound.X);
                }
            }
            if (input.CurPoint.Y >= _clientBound.Y - 5)
            {
                viewport.Y += SPEED_SCROLL.Y;
                if (viewport.Y > ((_nRow + 1) * ((int)CELL_SIZE.Y >> 1) - _clientBound.Y))
                {
                    viewport.Y = ((_nRow + 1) * ((int)CELL_SIZE.Y >> 1) - _clientBound.Y);
                }
            }

            this.TopLeft = viewport;
            
            for (int i = 0; i < _nRow; i++)
            {
                for (int j = 0; j < _nCol; j++)
                {
                    float x = (i % 2) * ((int)CELL_SIZE.X >> 1) + j * CELL_SIZE.X - TopLeft.X;
                    float y = i * ((int)CELL_SIZE.Y >> 1) - TopLeft.Y;
                    _Cell[i,j].TopLeft = new Vector2(x,y);
                    _Cell[i,j].Update(gameTime, input);
                }
            }
             
        }
        public override void Draw(GameTime gameTime, SpriteBatch spriteBatch)
        {
            for (int i = 0; i < _nRow; i++)
            {
                for (int j = 0; j < _nCol; j++)
                {
                    _Cell[i, j].Draw(gameTime,spriteBatch);
                }
            }
        }

    }
}
